package com.shangcheng.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.SmsHomeRecommendSubjectMapper;
import com.shangcheng.model.domain.SmsHomeNewProduct;
import com.shangcheng.model.domain.SmsHomeRecommendProduct;
import com.shangcheng.model.domain.SmsHomeRecommendSubject;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/13 14:30
 */
@DubboService
public class SmsHomeRecommendSubjectApiImpl implements SmsHomeRecommendSubjectApi{
    @Autowired
    private SmsHomeRecommendSubjectMapper smsHomeRecommendSubjectMapper;

    /**
     * 批量添加推荐专题
     * @param homeRecommendSubjectList
     */
    @Override
    public void createSubject(List<SmsHomeRecommendSubject> homeRecommendSubjectList) {
        for (SmsHomeRecommendSubject subject : homeRecommendSubjectList) {
            smsHomeRecommendSubjectMapper.insert(subject);
        }
    }

    /**
     * 批量删除推荐专题
     * @param ids
     */
    @Override
    public void deleteSubject(Integer[] ids) {
            smsHomeRecommendSubjectMapper.deleteBatchIds(Arrays.asList(ids.clone()));
    }

    /**
     * 分页查询推荐专题
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     */
    @Override
    public IPage<SmsHomeRecommendSubject> subjectList(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        IPage<SmsHomeRecommendSubject> page= new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(subjectName),SmsHomeRecommendSubject::getSubjectName,subjectName);
        wrapper.eq(!ObjectUtils.isEmpty(recommendStatus),SmsHomeRecommendSubject::getRecommendStatus,recommendStatus);
        return smsHomeRecommendSubjectMapper.selectPage(page,wrapper);
    }

    /**
     * 批量修改推荐状态
     * @param ids
     * @param recommendStatus
     */
    @Override
    public void updateSubjectStatus(Integer[] ids, Integer recommendStatus) {
        for (Integer id : ids) {
            SmsHomeRecommendSubject subject =new SmsHomeRecommendSubject();
            subject.setId(id.longValue());
            subject.setRecommendStatus(recommendStatus);
            smsHomeRecommendSubjectMapper.updateById(subject);
        }
    }

    /**
     * 修改推荐排序
     * @param id
     * @param sort
     */
    @Override
    public void updateSubjectSort(Integer id, Integer sort) {
        SmsHomeRecommendSubject subject=new SmsHomeRecommendSubject();
        subject.setId(id.longValue());
        subject.setSort(sort);
        LambdaUpdateWrapper<SmsHomeRecommendSubject> wrapper= new LambdaUpdateWrapper<>();
        wrapper.eq(SmsHomeRecommendSubject::getId,subject.getId());
        wrapper.set(!ObjectUtils.isEmpty(sort),SmsHomeRecommendSubject::getSort,subject.getSort());
        smsHomeRecommendSubjectMapper.update(subject,wrapper);
    }

    /**
     * 根据专题名称查询推荐专题
     * @param homeRecommendSubjectList
     * @return
     */
    @Override
    public List<SmsHomeRecommendSubject> findByName(List<SmsHomeRecommendSubject> homeRecommendSubjectList) {
        List<String> subjectNames = CollUtil.getFieldValues(homeRecommendSubjectList, "subjectName", String.class);
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper= new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeRecommendSubject::getSubjectName,subjectNames);
        return smsHomeRecommendSubjectMapper.selectList(wrapper);
    }
}
